package com.heyun.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;


/**
 * token生成与解码
 */
public class JwtUtil {

	//接收业务数据,生成token并返回
    public static String genToken(String secretKey, long ttlMillis, Map<String, Object> claims) {
        // 生成JWT的时间
        long expMillis = System.currentTimeMillis() + ttlMillis;
        Date exp = new Date(expMillis);
        return  JWT.create()
                    .withClaim("claims",claims)
                    .withExpiresAt(exp)
                    .sign(Algorithm.HMAC256(secretKey));

    }

	//接收token,验证token,并返回业务数据
    public static Map<String, Object> parseToken(String secretKey,String token) {
        return JWT.require(Algorithm.HMAC256(secretKey))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
